<?php
/**
 * Created by PhpStorm.
 * User: Dinh Thi
 * Date: 1/19/15
 * Time: 2:04 PM
 */

namespace MyApp\Application\Model;

use DinhThi\MVC\Model\MySQLModel;
use PDO;
use DinhThi\MVC\Configuration;

use Zend\Db\Adapter\Adapter as DbAdapter;
use Zend\Authentication\Adapter\DbTable as AuthAdapter;
use Zend\Authentication\AuthenticationService;
class UserModel extends MySQLModel{
    public function __construct(){
        parent::connect();
    }

    public function login($username = 'anonymous', $password) {
        //database driver config
        $configs = Configuration::getInstance();
        $host = $configs->getProperty('MysqlServer.host', 'localhost');
        $port = $configs->getProperty('MysqlServer.port', 3306);
        $database = $configs->getProperty('MysqlServer.database', 'buzzmetrics');

        $dbUsername = $configs->getProperty('MysqlServer.username', 'root');
        $dbPassword = $configs->getProperty('MysqlServer.password', '');

        $driver = array(
            'driver' => 'Pdo',
            'dsn' => 'mysql:dbname=' . $database . ';host=' . $host . ':' . $port,
            'driver_options' => array(
                PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\' ',
                PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
                PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            ),
            'username' => $dbUsername,
            'password' => $dbPassword
        );

        //database adapter
        $dbAdapter = new DbAdapter($driver);

        //auth adapter
        $authAdapter = new AuthAdapter($dbAdapter, 'users', 'username', 'password'
        //'MD5(MD5(CONCAT(?,user_id))) AND status_code = "1"'
        );

        //set input
        //$username = (null == $username) ? 'anonymous' : $username; //check if Identity is null
        $authAdapter
            ->setIdentity($username)
            ->setCredential(md5($password))
        ;

        //perform checking and remember
        $auth = new AuthenticationService();
        $result = $auth->authenticate($authAdapter);

        //if successful
        if ($result->isValid()) {
        //get user info ( get data from table) and save them
            $getInfo = $authAdapter->getResultRowObject(null, array('password')); //don't get password
            $auth->getStorage()->write($getInfo); //store them
        }

        return $result;
    }

    public function logout() {
        //set session
        $auth = new AuthenticationService();
        $auth->clearIdentity();
    }

    public function KiemTraMatKhau($id, $password){
        $sth = $this->DB->prepare("select * from users where id = :id and password = :password");
        $sth->execute(
            array(
                ':id'       => $id,
                ':password' => md5($password)
            )
        );
        return $sth->fetch();
    }

    public function DoiMatKhau($id, $password){
        $sth = $this->DB->prepare("update users set password = :password where id = :id");
        $sth->execute(
            array(
                ':password' => md5($password),
                ':id'       => $id
            )
        );
    }
} 